package com.xk.base.activity

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.viewbinding.ViewBinding
import com.xk.base.R
import com.xk.base.databinding.ActivityBaseBinding
import utils.CommonUtils
import utils.StatusBarUtil


open class BaseActivity:AppCompatActivity() {
    private var mBaseBinding: ActivityBaseBinding? = null
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

     fun setBind(bing: ViewBinding) {

        mBaseBinding = ActivityBaseBinding.inflate(layoutInflater)
        // content
        val params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        bing.root.layoutParams = params
        val mContainer = mBaseBinding!!.root.findViewById(R.id.container) as RelativeLayout
        mContainer.addView(bing.root)
        window.setContentView(mBaseBinding!!.root)
        setToolBar(mBaseBinding!!.toolBar)
        initStatusBar()
    }

    protected open fun setToolBar(toolbar: Toolbar) {
        setSupportActionBar(toolbar)
        val actionBar = supportActionBar
        if (actionBar != null) {
            //去除默认Title显示
            actionBar.setDisplayShowTitleEnabled(false)
            actionBar.setDisplayHomeAsUpEnabled(true)
            actionBar.setHomeAsUpIndicator(R.drawable.icon_back)
        }
        toolbar.setNavigationOnClickListener { supportFinishAfterTransition() }
    }

    protected open fun initStatusBar() {
        // 设置透明状态栏，兼容4.4
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.colorToolBar), 0)
    }
    open fun setNoTitle() {
        mBaseBinding!!.toolBar.visibility = View.GONE
    }

    override fun onResume() {
        super.onResume()
    }


}